/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnfMantenimientoContrasenha.java
 *
 * Created on Oct 18, 2011, 11:15:26 PM
 */

package visual.Seguridad;

import bentity.beLote;
import bentity.bePallet;
import bentity.beProducto;
import bentity.beProveedor;
import bentity.beTipoProducto;
import bentity.beUbicacion;
import bentity.beUsuario;
import blogic.blAlmacen;
import blogic.blIngreso;
import blogic.blLote;
import blogic.blPallet;
import blogic.blProducto;
import blogic.blProveedor;
import blogic.blRack;
import blogic.blUbicacion;
import blogic.blUsuario;
import java.io.File;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
//import java.util.Date;
import javax.swing.JFileChooser;
import jxl.Sheet;
import jxl.Workbook;

/**
 *
 * @author renzo
 */
public class pnfMantenimientoContrasenha extends javax.swing.JPanel {
    private beUsuario usuario = null;
    private String contrasenha = "";

    /** Creates new form pnfMantenimientoContrasenha */
    public pnfMantenimientoContrasenha(beUsuario usuario, String contrasenha) {
        initComponents();
        this.usuario = usuario;
        this.contrasenha = contrasenha;

        if (!(this.contrasenha.equals(""))){
            this.pwdContrasenhaAntigua.setText(this.contrasenha);
        }
    }

    public void mostrarMensajeCancelado(){
        visualHelper.mostrarMensajeError("Se canceló el acceso al sistema.");
    }

    public void mostrarMensajeInvalido(){
        visualHelper.mostrarMensajeError("El usuario o contraseña no son válidos");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        lblContrasenhaNueva = new javax.swing.JLabel();
        pwdContrasenhaNueva = new javax.swing.JPasswordField();
        lblReescribeContrasenha = new javax.swing.JLabel();
        pwdReescribeContrasenha = new javax.swing.JPasswordField();
        lblContrasenhaAntigua = new javax.swing.JLabel();
        pwdContrasenhaAntigua = new javax.swing.JPasswordField();
        btnRegistrar = new javax.swing.JButton();

        setLayout(null);

        lblTitulo.setFont(new java.awt.Font("Dialog", 0, 36));
        lblTitulo.setForeground(new java.awt.Color(0, 0, 0));
        lblTitulo.setText("Cambio de contraseña");
        add(lblTitulo);
        lblTitulo.setBounds(120, 50, 420, 43);

        lblContrasenhaNueva.setFont(new java.awt.Font("Dialog", 0, 12));
        lblContrasenhaNueva.setForeground(new java.awt.Color(0, 0, 0));
        lblContrasenhaNueva.setText("Contraseña nueva");
        add(lblContrasenhaNueva);
        lblContrasenhaNueva.setBounds(160, 170, 130, 15);

        pwdContrasenhaNueva.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdContrasenhaNuevaKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdContrasenhaNuevaKeyPressed(evt);
            }
        });
        add(pwdContrasenhaNueva);
        pwdContrasenhaNueva.setBounds(330, 170, 115, 28);

        lblReescribeContrasenha.setFont(new java.awt.Font("Dialog", 0, 12));
        lblReescribeContrasenha.setForeground(new java.awt.Color(0, 0, 0));
        lblReescribeContrasenha.setText("Reescribir contraseña");
        add(lblReescribeContrasenha);
        lblReescribeContrasenha.setBounds(160, 220, 150, 15);

        pwdReescribeContrasenha.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdReescribeContrasenhaKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdReescribeContrasenhaKeyPressed(evt);
            }
        });
        add(pwdReescribeContrasenha);
        pwdReescribeContrasenha.setBounds(330, 220, 115, 28);

        lblContrasenhaAntigua.setFont(new java.awt.Font("Dialog", 0, 12));
        lblContrasenhaAntigua.setForeground(new java.awt.Color(0, 0, 0));
        lblContrasenhaAntigua.setText("Contraseña antigua");
        add(lblContrasenhaAntigua);
        lblContrasenhaAntigua.setBounds(160, 120, 130, 15);

        pwdContrasenhaAntigua.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                pwdContrasenhaAntiguaKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdContrasenhaAntiguaKeyPressed(evt);
            }
        });
        add(pwdContrasenhaAntigua);
        pwdContrasenhaAntigua.setBounds(330, 120, 115, 28);

        btnRegistrar.setBackground(new java.awt.Color(240, 235, 226));
        btnRegistrar.setFont(new java.awt.Font("Dialog", 0, 12));
        btnRegistrar.setForeground(new java.awt.Color(0, 0, 0));
        btnRegistrar.setText("Registrar");
        btnRegistrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRegistrarActionPerformed(evt);
            }
        });
        add(btnRegistrar);
        btnRegistrar.setBounds(260, 280, 110, 27);
    }// </editor-fold>//GEN-END:initComponents

    private void btnRegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRegistrarActionPerformed

        try{            
            if (this.pwdContrasenhaAntigua.getPassword().length==0 || this.pwdContrasenhaNueva.getPassword().length==0 ||
                    this.pwdReescribeContrasenha.getPassword().length==0){
                visualHelper.mostrarMensajeError("Debe completar todos los campos.");
                return;
            }

            encriptado encriptador = new encriptado();

            if (!(this.usuario.getContrasenha().equals(encriptador.encriptar(String.valueOf(this.pwdContrasenhaAntigua.getPassword()))))){
                visualHelper.mostrarMensajeError("La contrasenha ingresada es incorrecta.");
                return;
            }

            if (!(Arrays.equals(this.pwdContrasenhaNueva.getPassword(),this.pwdReescribeContrasenha.getPassword()))){
                visualHelper.mostrarMensajeError("Las contraseñas ingresadas deben ser iguales.");
                return;
            }

            if (this.pwdContrasenhaNueva.getPassword().length<5 ||
                this.pwdReescribeContrasenha.getPassword().length<5){
                visualHelper.mostrarMensajeError("La contraseña nueva debe tener como mínimo 5 caracteres,");
                return;
            }

            this.usuario.setContrasenha(encriptador.encriptar(this.pwdContrasenhaNueva.getText()));
            blUsuario gestorUsuarios = new blUsuario();
            gestorUsuarios.modificar(this.usuario);
            visualHelper.mostrarMensajeSatisfaccion("Se modificó la contrasenha satisfactoriamente.");
            this.limpiarCampos();
        }
        catch (Exception e){
            e.printStackTrace();
            visualHelper.mostrarMensajeError("No se pudo cambiar la contrasenha.");
        }
}//GEN-LAST:event_btnRegistrarActionPerformed

    private void pwdContrasenhaAntiguaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdContrasenhaAntiguaKeyPressed
        // TODO add your handling code here:
        visualHelper.deshabilitaCtrlV(evt);
    }//GEN-LAST:event_pwdContrasenhaAntiguaKeyPressed

    private void pwdContrasenhaNuevaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdContrasenhaNuevaKeyPressed
        // TODO add your handling code here:
        visualHelper.deshabilitaCtrlV(evt);
    }//GEN-LAST:event_pwdContrasenhaNuevaKeyPressed

    private void pwdReescribeContrasenhaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdReescribeContrasenhaKeyPressed
        // TODO add your handling code here:
        visualHelper.deshabilitaCtrlV(evt);
    }//GEN-LAST:event_pwdReescribeContrasenhaKeyPressed

    private void pwdContrasenhaAntiguaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdContrasenhaAntiguaKeyTyped
        // TODO add your handling code here:
        visualHelper.soloTextoyNumeros(evt);
    }//GEN-LAST:event_pwdContrasenhaAntiguaKeyTyped

    private void pwdContrasenhaNuevaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdContrasenhaNuevaKeyTyped
        // TODO add your handling code here:
        visualHelper.soloTextoyNumeros(evt);
    }//GEN-LAST:event_pwdContrasenhaNuevaKeyTyped

    private void pwdReescribeContrasenhaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdReescribeContrasenhaKeyTyped
        // TODO add your handling code here:
        visualHelper.soloTextoyNumeros(evt);
    }//GEN-LAST:event_pwdReescribeContrasenhaKeyTyped

    
    public void limpiarCampos(){
        this.pwdContrasenhaAntigua.setText("");
        this.pwdContrasenhaNueva.setText("");
        this.pwdReescribeContrasenha.setText("");
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnRegistrar;
    private javax.swing.JLabel lblContrasenhaAntigua;
    private javax.swing.JLabel lblContrasenhaNueva;
    private javax.swing.JLabel lblReescribeContrasenha;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JPasswordField pwdContrasenhaAntigua;
    private javax.swing.JPasswordField pwdContrasenhaNueva;
    private javax.swing.JPasswordField pwdReescribeContrasenha;
    // End of variables declaration//GEN-END:variables

}
